package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;

import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;

    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Menu menu = menuService.findById(id);
            return new Result(true, MessageConstant.GET_MENU_SUCCESS, menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }


    }

    /**
     * 删除菜单
     */
    @RequestMapping("/delete")
    public Result delete(Integer id) {

        try {
            menuService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MENU_FAIL);
        }
    }


    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")

    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return menuService.findPage(queryPageBean);
    }

    /**
     * 编辑菜单
     *
     * @param menu
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu) {
        try {
            menuService.editMenu(menu);
            return new Result(true, MessageConstant.EDIT_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MEMBER_FAIL);
        }
    }

    /**
     * 添加菜单
     *
     * @param menu
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu) {
        try {
            menuService.addMenu(menu);
            return new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MEMBER_FAIL);
        }
    }


    /**
     * 动态显示菜单
     *
     * @param username
     * @return
     */
    @RequestMapping("/showMenu")
    public Result getMenuList(String username) {

        try {
            List<Menu> menuList = menuService.findListByUsername(username);
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
            if (menuList != null) {
                Menu menu_0 = new Menu();
                menu_0.setPath("1");
                menu_0.setName("工作台");
                menu_0.setIcon("fa-dashboard");
                menu_0.setChildren(new ArrayList<Menu>());//不能为null
                menuList.add(0, menu_0);
                for (Menu menu : menuList) {
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("path", menu.getPath());
                    map.put("title", menu.getName());
                    map.put("icon", menu.getIcon());
                    List<Menu> children = menu.getChildren();
                    if (children != null && children.size() > 0) {
                        List<Map<String, Object>> children_map = doMenuChildren(children);
                        map.put("children", children_map);
                    } else {
                        map.put("children", new ArrayList<>());
                    }
                    list.add(map);
                }
            }
            return new Result(true, MessageConstant.GET_MENU_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }

    private List<Map<String, Object>> doMenuChildren(List<Menu> children) {
        List<Map<String, Object>> menu_children = new ArrayList<Map<String, Object>>();

        for (Menu child : children) {
            Map<String, Object> map_son = new HashMap<>();
            map_son.put("path", child.getPath());
            map_son.put("title", child.getName());
            String linkUrl = child.getLinkUrl();
            if (linkUrl != null && linkUrl.length() > 0) {
                map_son.put("linkUrl", linkUrl);
            }
            List<Menu> children_grand = child.getChildren();
            if (children_grand != null && children.size() > 0) {
                List<Map<String, Object>> grand_map = this.doMenuChildren(children_grand);
                map_son.put("children", grand_map);
            } else {
                map_son.put("children", new ArrayList<>());
            }
            menu_children.add(map_son);
        }
        return menu_children;
    }
}